package com.juicygames.shmup.world;

import android.view.MotionEvent;

import com.juicygames.framework.action.UpdateAction;
import com.juicygames.framework.draw.sprite.SpriteDrawer;
import com.juicygames.framework.gamepart.GameHandler;
import com.juicygames.framework.handler.UpdateHandler;
import com.juicygames.framework.property.space.Position;
import com.juicygames.framework.touch.WorldTouchAction;
import com.juicygames.shmup.player.Follower;
import com.juicygames.shmup.player.Player;
import com.juicygames.shmup.player.PlayerTouchHandler;
import com.juicygames.shmup.player.anim.StandAnim;
import com.juicygames.shmup.player.anim.WalkAnim;
import com.juicygames.shmup.target.Target;

public class WorldTouchHandler extends GameHandler<WorldTouchAction> {
	private static final String LOGTAG = WorldTouchHandler.class.getName();
	
	Target target;
	Player player;
	
	Position targetPosition;
	Follower playerFollower;
	SpriteDrawer playerSpriteDrawer;
	
	PlayerTouchHandler playerTouchHandler;
	
	MyUpdate myUpdateHandler;
	
	public void load() {
		target = parent.getPart(Target.class);
		player = parent.getPart(Player.class);
		
		targetPosition = target.getPart(Position.class);
		playerFollower = player.getPart(Follower.class);
		playerSpriteDrawer = player.getPart(SpriteDrawer.class);
		
		playerTouchHandler = player.getPart(PlayerTouchHandler.class);
		
		myUpdateHandler = addPart(new MyUpdate()); 
	}
	
	public Class<WorldTouchAction> getActionClassHandled() {
		return WorldTouchAction.class;
	}
	
	public void handle(WorldTouchAction touch) {
		
		MotionEvent event = touch.getEvent();
		
//		Log.d(LOGTAG,"World handling touch. event is " + event);
		
		if (event.getAction() == MotionEvent.ACTION_DOWN && !touch.isEntityWasTouched()) {
			handleTouchPress(touch);
		}
		else if (event.getAction() == MotionEvent.ACTION_UP) {
			handleTouchRelease(touch);
		}
		else {
			handleTouchDrag(touch);
		}
		
	}
	
	private void handleTouchPress(WorldTouchAction touch) {
		myUpdateHandler.setTouchToMoveTo(touch);
		
		target.setVisible(true);
		
		playerFollower.follow(targetPosition, 6);
		playerSpriteDrawer.setAnimation(WalkAnim.class);
	}
	
	private void handleTouchDrag(WorldTouchAction touch) {
		myUpdateHandler.setTouchToMoveTo(touch);
	}
	
	private void handleTouchRelease(WorldTouchAction touch) {
		target.setVisible(false);
		playerFollower.stopFollowing();
		playerSpriteDrawer.setAnimation(StandAnim.class);
		
		playerTouchHandler.handleGlobalTouchRelease(touch);
	}
	
	
	public class MyUpdate extends UpdateHandler {
		
		private WorldTouchAction touchToMoveTo = null;
		
		public void handle(UpdateAction update) {
			
			if (touchToMoveTo != null) {
				
				MotionEvent event = touchToMoveTo.getEvent();
				targetPosition.get().x = (int)event.getX();
				targetPosition.get().y = (int)event.getY();
				
				touchToMoveTo = null;
			}
		}

		public WorldTouchAction getTouchToMoveTo() {
			return touchToMoveTo;
		}

		public void setTouchToMoveTo(WorldTouchAction touchToMoveTo) {
			this.touchToMoveTo = touchToMoveTo;
		}
	}

}
